<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('INTERPHOTO')) die('File not found!');


// ######################################################

include('InterPhoto.GlobalFunctions.php');

// ###############################################################

function GetUrl($PageName, $Key ='', $Value ='', $PageNum='', $Querystring='') {
	global $mainsettings;

	if ($PageName == './') $PageName = 'index.php';
	$sReturn = '';

	if ($mainsettings['siteRewrite']) {
		$PageName = substr($PageName, 0, -4)	.Iif($Value, '-'.$Value, '').'.html';

		if ($PageNum != '') {
			$sReturn = 'p='.$PageNum;
		}
	} else {
		$sReturn = ($Value != '' && $Value != '0' ? $Key.'='.$Value : '');
		if ($PageNum != '') {
			if ($sReturn != '') $sReturn .= '&';
			$sReturn .= 'p='.$PageNum;
		}
	}

	if ($Querystring != '' && substr($Querystring, 0, 1) != '#') {
		if ($sReturn != '') $sReturn .= '&';
		$sReturn .= $Querystring;
	}
	if ($sReturn != '') $sReturn = '?'.$sReturn;
	if ($Querystring != '' && substr($Querystring, 0, 1) == '#') $sReturn .= $Querystring;
	return BASEURL.$PageName.$sReturn;
}

// ###############################################################

function GetPageList($FileName, $PageCount, $CurrentPage = 1, $PagesToDisplay = 10, $Key = '', $Value = '', $PN01 = '', $PNV01 = '', $PN02 = '', $PNV02 = '', $PN03 = '', $PNV03 = '', $PN04 = '', $PNV04 = '') {

	$PreviousText =  '&nbsp;&#60;&#60;&nbsp;';
	$NextText = '&nbsp;&#62;&#62;&nbsp;';

	$Params = '';
	$Params .= Iif($PN01 AND $PNV01, $PN01.'='.$PNV01);
	$Params .= Iif($PN02 AND $PNV02, Iif($Params, '&') . $PN02.'='.$PNV02);
	$Params .= Iif($PN03 AND $PNV03, Iif($Params, '&') . $PN03.'='.$PNV03);
	$Params .= Iif($PN04 AND $PNV04, Iif($Params, '&') . $PN04.'='.$PNV04);

	$iPagesToDisplay = $PagesToDisplay - 2;      
	if ($iPagesToDisplay <= 8) $iPagesToDisplay = 8;

	$MidPoint = ($iPagesToDisplay / 2);

	$FirstPage = $CurrentPage - $MidPoint;
	if ($FirstPage < 1) $FirstPage = 1;

	$LastPage = $FirstPage + ($iPagesToDisplay - 1);

	if ($LastPage > $PageCount) {
		$LastPage = $PageCount;
		$FirstPage = $PageCount - $iPagesToDisplay;
		if ($FirstPage < 1) $FirstPage = 1;
	}

	$sReturn = '<div class="PageListDiv"><ol class="PageList">';
	$Loop = 0;
	$iTmpPage = 0;

	if ($PageCount > 1) {
		if ($CurrentPage > 1) {
			$iTmpPage = $CurrentPage - 1;
			$sReturn .= '<li><a href="' . GetUrl($FileName,$Key,$Value,$iTmpPage, $Params) . '" class="PagePrev"  onfocus="this.blur()">'.$PreviousText.'</a></li>';
		} else {
			$sReturn .= '<li><span class="NoPagePrev">'.$PreviousText.'</span></li>';
		}

		if ($FirstPage > 2) {
			$sReturn .= '&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,1, $Params) . '" onfocus="this.blur()">1</a></li>&nbsp;<li>...</li>';
		} elseif ($FirstPage == 2) {
			$sReturn .= '&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,1, $Params) . '" onfocus="this.blur()">1</a></li>';
		}

		$Loop = 0;

		for ($Loop = 1; $Loop <= $PageCount; $Loop++) {
			if (($Loop >= $FirstPage) && ($Loop <= $LastPage)) {
				if ($Loop == $CurrentPage) {
					$sReturn .= '&nbsp;<li><span class="CurrentPage">'.$Loop.'</span></li>';
				} else {
					$sReturn .= '&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,$Loop, $Params) . '" onfocus="this.blur()">'.$Loop.'</a></li>';
				}
			}
		}

		if ($CurrentPage < ($PageCount - $MidPoint) && $PageCount > $PagesToDisplay - 1) {
			$sReturn .= '&nbsp;<li>...</li>&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,$PageCount, $Params) . '" onfocus="this.blur()">'.$PageCount.'</a></li>';
		} else if ($CurrentPage == ($PageCount - $MidPoint) && ($PageCount > $PagesToDisplay)) {
			$sReturn .= '&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,$PageCount, $Params) . '" onfocus="this.blur()">'.$PageCount.'</a></li>';
		}

		if ($CurrentPage != $PageCount) {
			$iTmpPage = $CurrentPage + 1;
			$sReturn .= '&nbsp;<li><a href="' . GetUrl($FileName,$Key,$Value,$iTmpPage, $Params) . '" class="PageNext" onfocus="this.blur()">'.$NextText.'</a></li>';
		} else {
			$sReturn .= '&nbsp;<li><span class="NoPageNext">'.$NextText.'</span></li>';
		}
	} else {
		$sReturn .= '<li>&nbsp;</li>';
	}

	$sReturn .= '</ol></div>';

	return  $sReturn;
}

// ###############################################################

function GetSystemInfo(){
	global $errors, $errortitle, $success, $successtitle;

	if(isset($errors)){
		if(is_array($errors))
		{
			for($i = 0; $i < count($errors); $i++)
				$errorinfo .= ($i + 1) . ') ' . $errors[$i] . '<br />';
		}else {
			$errorinfo = $errors . '<br />';
		}

		return '<div id="sysinfo_error"><div class="e_lborder"><div class="e_rborder"><div class="e_bborder"><div class="e_blcorner"><div class="e_brcorner"><div class="e_tborder"><div class="e_tlcorner"><div class="e_trcorner">'.Iif($errortitle, '<B><U>'.$errortitle.':</U></B><BR><BR>').$errorinfo.'	</div></div></div></div></div></div></div></div></div>';
	}elseif(isset($success)){
		if(is_array($success))
		{
			for($i = 0; $i < count($success); $i++)
				$successinfo .= ($i + 1) . ') ' . $success[$i] . '<br />';
		}else {
			$successinfo = $success . '<br />';
		}

		return '<div id="sysinfo_success"><div class="s_lborder"><div class="s_rborder"><div class="s_bborder"><div class="s_blcorner"><div class="s_brcorner"><div class="s_tborder"><div class="s_tlcorner"><div class="s_trcorner">'.Iif($successtitle, '<B><U>'.$successtitle.':</U></B><BR><BR>').$successinfo.'</div></div></div></div></div></div></div></div></div>';
	}else{
		return '';
	}

}


// ###############################################################

function CreateVVC() {
	global $DB;

	$DB->query("INSERT INTO " . TABLE_PREFIX . "vvc VALUES (NULL, '', '".time()."')");

	return $DB->insert_id();
}

// ###############################################################

function CheckVVC($vvcid, $code) {
	global $DB;

	if(!$code){return false;}

	if(is_numeric($vvcid)){
		$verifycode = $DB->query_first("SELECT code FROM " . TABLE_PREFIX . "vvc WHERE vvcid = '$vvcid'");

		if($verifycode['code'] == strtoupper($code)){
			$DB->query("DELETE FROM " . TABLE_PREFIX . "vvc WHERE vvcid = '$vvcid'");
			return true;
		}
	}
	return false;
}


// ##############################################################

function GetUserInfo($userid)
{
	global $DB;

	$userinfo = array();

	if(!empty($userid) AND $userid > 0) {

		$userinfo = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "users u LEFT JOIN  " . TABLE_PREFIX . "usergroups ug ON (u.groupid = ug.groupid) WHERE u.activated = 1 AND u.userid = '$userid' ");

		$userinfo['password'] = '';
		$userinfo['loggedin'] = 1;

	} else {
		// Get guest users info.
		$userinfo = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "usergroups WHERE groupid = 1 ");
		$userinfo['userid'] = 0;
	}

	return $userinfo;
}


// ###############################################################

function CheckAccess($Access = 'noAccess') {
	global $userinfo;

	if ($userinfo['userid']) {
		if (ForceInt($userinfo[''.$Access]) != 1) {
			if (ForceInt($userinfo['allowlogin'])){
				header("Location: ".GetUrl('mydesk.noaccess.php'));
				exit();
			}else{
				setcookie(COOKIE_NAME, "", 0, "/");
				header("Location: ".GetUrl('login.php'));
				exit();
			}
		}
	}else{
		header("Location: ".GetUrl('login.php'));
		exit();
	}
}

// ###############################################################

function GetContent($id) {
	global $DB;

	$contentid = ForceInt($id);
	$getcontent = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "contents WHERE contentid = '$contentid'");
	return $getcontent;
}

?>